package com.du.fileload.framework.config;

import org.apache.commons.codec.binary.Base64;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * 
 * @author Du
 * 
 */
@Component
@ConfigurationProperties(prefix = "jwt")
public class JWTConfig {

	/**
	 * 数据请求返回码
	 */
	public static final int RESCODE_SUCCESS = 1000; // 成功
	public static final int RESCODE_SUCCESS_MSG = 1001; // 成功(有返回信息)
	public static final int RESCODE_EXCEPTION = 1002; // 请求抛出异常
	public static final int RESCODE_NOLOGIN = 1003; // 未登陆状态
	public static final int RESCODE_NOEXIST = 1004; // 查询结果为空
	public static final int RESCODE_NOAUTH = 1005; // 无操作权限

	/**
	 * 由字符串生成加密key
	 */
	private SecretKey key;

	private String secret = "7786df7fc3a34e26a61c034d5ec8245d";

	private int expiration = 60 * 60 * 1000;

	private String tokenHead;

	/**
	 * 用户信息存放key
	 */
	private String mainKey="key";

	//全局缓存实例
	private static JWTConfig GlobalJWTConfig;

	public static JWTConfig getInstance(){
		return GlobalJWTConfig;
	}

	public JWTConfig(){
		if(JWTConfig.GlobalJWTConfig==null)
			JWTConfig.GlobalJWTConfig=this;
	}


	public String getSecret() {
		return secret;
	}

	public void setSecret(String secret) {
		byte[] encodedKey = Base64.decodeBase64(secret);
		key = new SecretKeySpec(encodedKey, 0, encodedKey.length, "HmacSHA256");
		this.secret = secret;
	}

	public int getExpiration() {
		return expiration;
	}

	public void setExpiration(int expiration) {
		this.expiration = expiration;
	}

	public  SecretKey getKey() {
		return key;
	}

	public String getMainKey() {
		return mainKey;
	}

	public void setMainKey(String mainKey) {
		this.mainKey = mainKey;
	}

	public String getTokenHead() {
		return tokenHead;
	}

	public void setTokenHead(String tokenHead) {
		this.tokenHead = tokenHead;
	}
}
